//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbLoaiTinTuc
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLoaiTinTuc_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLoaiTinTuc_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbLoaiTinTuc OTbLoaiTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLoaiTT", OTbLoaiTinTuc.TenLoaiTT);
            pr[1] = new SqlParameter(@"TrangThai", OTbLoaiTinTuc.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLoaiTinTuc_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbLoaiTinTuc OTbLoaiTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaLoaiTT", OTbLoaiTinTuc.MaLoaiTT);
            pr[1] = new SqlParameter(@"TenLoaiTT", OTbLoaiTinTuc.TenLoaiTT);
            pr[2] = new SqlParameter(@"TrangThai", OTbLoaiTinTuc.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLoaiTinTuc_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaLoaiTT)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLoaiTT",MaLoaiTT);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbLoaiTinTuc_Delete", pr);
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbLoaiTinTuc LoaiTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLoaiTT", LoaiTinTuc.TenLoaiTT);
            pr[1] = new SqlParameter(@"MaLoaiTT", LoaiTinTuc.MaLoaiTT);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLoaiTinTuc_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbLoaiTinTuc LoaiTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenLoaiTT", LoaiTinTuc.TenLoaiTT);
            
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLoaiTinTuc_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static DataTable SearchLoaiTinTuc(ETbLoaiTinTuc LoaiTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenLoaiTT", LoaiTinTuc.TenLoaiTT);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLoaiTinTuc_Search", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static ETbLoaiTinTuc SelectByID(int MaLoaiTT)
        {
            ETbLoaiTinTuc OTbLoaiTinTuc = new ETbLoaiTinTuc();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLoaiTT",MaLoaiTT);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbLoaiTinTuc_SelectByID", pr);
            if (idr.Read())
              OTbLoaiTinTuc = GetOneTbLoaiTinTuc(idr);
            idr.Close();
            idr.Dispose();
            return OTbLoaiTinTuc;
        }
//-----------------------------------------------------------//
        private static ETbLoaiTinTuc GetOneTbLoaiTinTuc(IDataReader idr)
       {
            ETbLoaiTinTuc OTbLoaiTinTuc = new ETbLoaiTinTuc();
                if (idr["MaLoaiTT"] != DBNull.Value)
                    OTbLoaiTinTuc.MaLoaiTT = (int)idr["MaLoaiTT"];
                if (idr["TenLoaiTT"] != DBNull.Value)
                    OTbLoaiTinTuc.TenLoaiTT = (string)idr["TenLoaiTT"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbLoaiTinTuc.TrangThai = (bool)idr["TrangThai"];
            return OTbLoaiTinTuc;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaLoaiTT)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaLoaiTT",MaLoaiTT);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLoaiTinTuc_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbLoaiTinTuc> ListAll()
        {
            List<ETbLoaiTinTuc> list = new List<ETbLoaiTinTuc>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbLoaiTinTuc_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbLoaiTinTuc(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbLoaiTinTuc> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbLoaiTinTuc> list = new List<ETbLoaiTinTuc>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbLoaiTinTuc_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbLoaiTinTuc(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}